<?php
namespace src;
use classlib\input;
use src\logic\Cadmin;
use classlib\core as C;
use classlib\core\controller;
use function classlib\core\cut_str;
class Csitemsg extends controller {
	public function main() {
		$view  = C\view::inst('extjs');
		Cadmin::chkaccess($view);
		$lang  = C\lang::inst('adm');
		$dlang = C\lang::inst('dd');
		$dictid= \src\logic\lconfig::dd('system_sitemsg_types');
		$view->assign('dictid', $dictid);
		$view->assign('status', $dlang->get('sh0008'));
		$view->assign_by_ref('title', Cadmin::$name);
		$view->display('live/sitemsg.ext.js');
	}
	
	/**
	 * @desc    新增以及编辑站内信资料信息
	 * @doc     编辑资料
	 * @access  需要授权 
	 */
	public function dosave() {
		$view   = C\view::inst('json');
		Cadmin::chkaccess($view);
		$data   = array('stime'=>C\config::$nowtime);
		$filter = new \classlib\validator\auditor();
		$lang   = C\lang::inst('adm');
		$filter->lang($lang);
		$id     = (int)input::get_post('id');
		try {
			if ($id < 1) {
				$data['senderid'] = 0;
			}
			$data['status']    = $filter->reset('status')->numeric()->min(0)->run();
			$data['reveiverid']= $filter->reset('reveiverid')->numeric()->min(0)->run();
			$data['dictid']    = $filter->reset('dictid')->numeric()->min(0)->run();
			$data['title']     = $filter->reset('title')->lbetween(1, 255)->run();
			$data['msg']       = $filter->reset('msg')->minlen(1)->run();
		} catch (\classlib\validator\AuditorException $e) {
			$view->assign_error($e->getField(), $e->getMessage());
		} catch (\Exception $e) {
			$view->assign_error($e->getCode(), $e->getMessage());
		}
		$orm    = C\model::load('pro.pl.msg');
		($id > 0)? $orm->save($id, $data):$orm->new_one($data);
		Cadmin::log2db(C\view::inst('xml')->array2xml($data));
		$view->display();
	}
	
	/**
	 * 获取资料列表
	 */
	public function dolist() {
		$view   = C\view::inst('json');
		Cadmin::chkaccess($view);
		list($start, $limit, $sort) = Cadmin::offset();
		$where    = array();
		$senderid = input::get_post('senderid');
		if (is_numeric($senderid)) {
			$where[] = array('senderid', $senderid, \classlib\orm::OP_EQ);
		}
		$reveiverid = input::get_post('reveiverid');
		if (is_numeric($reveiverid)) {
			$where[] = array('reveiverid', $reveiverid, \classlib\orm::OP_EQ);
		}
		$dictid = input::get_post('dictid');
		if (is_numeric($dictid)) {
			$where[] = array('dictid', $dictid, \classlib\orm::OP_EQ);
		}
		$query  = input::get_post('query');
		$query  = trim($query);
		if ($query && !empty($query)) {
			$where[] = array('title', '%'.$query.'%', \classlib\orm::OP_LIKE);
		}
		$orm    = C\model::load('pro.pl.msg');
		$total  = $orm->get_total($where);
		$list   = array();
		if ($total) {
			$list   = $orm->get_list($start, $limit, $where, '*', $sort);
			$uorm   = C\model::load('pro.pl.userinfo');
			foreach ($list as &$item) {
				$item['notes']  = cut_str(strip_tags($item['msg']), 150);
 				$item['stime']  = date('Y-m-d H:i:s', $item['stime']);
				$item['sender'] = '系统发送[SystemSender]';
				if ($item['senderid'] > 0) {
					$user 			= $uorm->get_one($item['senderid']);
					$item['sender'] = $user['nickname'].'['.$user['email'].']';
				}
				$item['reveiver'] = '全站接收[AllUser]';
				if ($item['reveiverid'] > 0) {
					$user 			  = $uorm->get_one($item['reveiverid']);
					$item['reveiver'] = $user['nickname'].'['.$user['email'].']';
				}
			}
		}
		$view->assign_by_ref('total', $total);
		$view->assign_by_ref('dataset', $list);
		$view->display();
	}
}